package com.zhc.dejingapi.utils.common;

import com.google.gson.Gson;
import com.zhc.dejingapi.entity.BommakeEntity;
import com.zhc.dejingapi.pojo.common.CommonDelMoRoutingDataModel;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.appdata.deldata.QingDelDataModel;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.utils.qing.QingflowAPIUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CommonOrderBackToLakeUtils {

    public static QingCommonBackResult DeleteMoRoutingDetails(CommonDelMoRoutingDataModel param, QingflowApiInfo qqingflowApiInfo, JdbcTemplate mySqlQFlowJdbcTemplate) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();

        commonBackResult.setErrCode(0);
        commonBackResult.setErrMsg("");

        String strSql = "\n" +
                "SELECT apply_id\n" +
                "from `data` \n" +
                "where form_id=? and que_id=? and IFNULL(`value`,'')=?";
        List<Long> listApply_id = mySqlQFlowJdbcTemplate.queryForList(strSql, Long.class,
                param.getMoRoutingAppFormId(),
                param.getMoRoutingAppOrderIdFieldQueId(),
                param.getOrderAppDelQue0Value()
                );

        if (listApply_id == null || listApply_id.isEmpty()) {

        } else {
            commonBackResult = DelDataByApplyId(listApply_id, qqingflowApiInfo, param.getMoRoutingAppKey());
        }

        return commonBackResult;
    }

    public static QingCommonBackResult DelDataByApplyId(List<Long> listApplyId, QingflowApiInfo qqingflowApiInfo, String appKey) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();
        commonBackResult.setErrCode(0);
        commonBackResult.setErrMsg("");

        try {
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingDelDataModel qingAddDataModel = new QingDelDataModel();
            qingAddDataModel.setApplyIds(listApplyId);
            //转字符串
            //String strJson= JSON.toJSONString(qingAddDataModel);
            Gson gson = new Gson();
            String strJson = gson.toJson(qingAddDataModel);
            //获取返回结果
            //qingflowApiInfo qingflowApiInfo, String appKey,String strBody
            QingAddDataResultMain resultMain = QingflowAPIUtils.DelDataToQing(qqingflowApiInfo, appKey, strJson);

            if (resultMain == null) {

                commonBackResult.setErrCode(-10015);
                commonBackResult.setErrMsg("服务器返回内容为空！");

            } else {
                if (resultMain.getErrCode() == 0) {

                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg("删除成功！");

                    //增加马上判断的结果
                    QingQueryActionResultBase actionResultBase = QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,
                            resultMain.getResult().getRequestId());
                    if (actionResultBase == null) {
                        commonBackResult.setErrCode(-10066);
                        commonBackResult.setErrMsg("返回出错：返回内容为空！");

                    } else {
                        if (actionResultBase.getErrorCode() == 0) {

                        } else {
                            commonBackResult.setErrCode(actionResultBase.getErrorCode());
                            commonBackResult.setErrMsg(actionResultBase.getMessage());

                        }
                    }
                } else {

                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg(resultMain.getErrMsg());
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            //throw  e;
            commonBackResult.setErrCode(-10010);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace().toString());
            //要删除应用的这条记录
        }
        return commonBackResult;
    }

    public static QingCommonBackResult DelDataByApplyId(Long lApplyId, QingflowApiInfo qqingflowApiInfo, String appKey) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();
        commonBackResult.setErrCode(0);
        commonBackResult.setErrMsg("");

        try {
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            List<Long> listApplyId = new ArrayList<>();
            listApplyId.add(lApplyId);
            QingDelDataModel qingAddDataModel = new QingDelDataModel();
            qingAddDataModel.setApplyIds(listApplyId);
            //转字符串
            //String strJson= JSON.toJSONString(qingAddDataModel);
            Gson gson = new Gson();
            String strJson = gson.toJson(qingAddDataModel);
            //获取返回结果
            //qingflowApiInfo qingflowApiInfo, String appKey,String strBody
            QingAddDataResultMain resultMain = QingflowAPIUtils.DelDataToQing(qqingflowApiInfo, appKey, strJson);

            if (resultMain == null) {

                commonBackResult.setErrCode(-10015);
                commonBackResult.setErrMsg("服务器返回内容为空！");

            } else {
                if (resultMain.getErrCode() == 0) {

                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg("删除成功！");

                    //增加马上判断的结果
                    QingQueryActionResultBase actionResultBase = QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,
                            resultMain.getResult().getRequestId());
                    if (actionResultBase == null) {
                        commonBackResult.setErrCode(-10066);
                        commonBackResult.setErrMsg("返回出错：返回内容为空！");

                    } else {
                        if (actionResultBase.getErrorCode() == 0) {

                        } else {
                            commonBackResult.setErrCode(actionResultBase.getErrorCode());
                            commonBackResult.setErrMsg(actionResultBase.getMessage());

                        }
                    }
                } else {

                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg(resultMain.getErrMsg());
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            //throw  e;
            commonBackResult.setErrCode(-10010);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace().toString());
            //要删除应用的这条记录
        }
        return commonBackResult;
    }


    private static final double EPSILON = 1e-8; // 误差范围

    public static boolean doubleIsEqual(double val1, double val2) {
        return Math.abs(val1 - val2) < EPSILON;
    }

    public static boolean doubleIsGreaterThan(double val1, double val2) {
        return val1 - val2 > EPSILON;
    }

    public static boolean doubleIsLessThan(double val1, double val2) {
        return val2 - val1 > EPSILON;
    }

    public static boolean doubleIsGreaterThanOrEqualTo(double val1, double val2) {
        return val1 - val2 >= -EPSILON;
    }

    public static boolean doubleIsLessThanOrEqualTo(double val1, double val2) {
        return val2 - val1 >= -EPSILON;
    }


}
